package com.dhecp.project.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.project.common.domain.UpdateLog;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.storage.domain.Dismounting;

/**
 * @program: kjecp
 * @description: 数据变更记录表
 * @author: hlf
 * @create: 2022-09-19 09:39
 */
@Controller
@RequestMapping("/common/updatelog")
public class UpdateLogController extends BaseModuleController {

    private String prefix = "common/updatelog";
    private String moduleTable = ModuleTable.UPDATE_LOG;

    @Autowired
    private UpdateLogService updateLogService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    /**
     *@Description: 打开查询列表页
     *@Param: [params]
     *@return: org.springframework.web.servlet.ModelAndView
     *@Author: hlf
     *@date: 2022-05-09 11:39
     */
    @RequiresPermissions("common:updatelog:view")
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new Dismounting()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("prefix", prefix + "/updatelog"); //页面路径
        params.put("toolBarWidth", 0);
//        params.put("checkBox", "yes");

        ModelAndView mv = this.openListPage(params);
        mv.addObject("storage", "dismounting");
        return mv;
    }

    /**
     *@Description: 查询页 列表数据
     *@Param: [params]
     *@return: com.dhecp.common.core.page.TableDataInfo
     *@Author: hlf
     *@date: 2022-05-09 11:41
     */
    @RequiresPermissions("common:updatelog:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        List<UpdateLog> list = new ArrayList<UpdateLog>();
        try {
            list = updateLogService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
     * @Description: 查询页 子列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: hlf
     * @date: 2022-08-15 15:43
     */
    @RequiresPermissions("common:updatelog:list")
    @GetMapping("/selectLog")
    @ResponseBody
    public AjaxResult selectLog(@RequestParam Map<String, Object> params) {
        try {
            params.put("moduleTable", moduleTable);
            params.put("entity", new UpdateLog()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
            params.put("toolBarWidth", 0);
            AjaxResult ajaxResult = new AjaxResult();
            Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
            Map<String, Object> mapList = new HashMap<>();
            mapList.put("list", updateLogService.queryList(params));
            mapList.put("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
            mapList.put("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
            ajaxResult = AjaxResult.success(mapList);
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(this.getExceptionMessage(e));
        }
    }
}